<?php
namespace App\Http\Controllers\Backend;
use Aitifen\Library\Xxtea\Xxtea;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;


class AssistantController extends Controller
{
    /**
     * @param Request $request
     * 添加讲师助教
     * user_type 1助教 2讲师
     */
    public function savaAssistant ( Request $request )
    {

       try{

           $ass_data = $request->all();
           $r = $this->__Valdate($ass_data);
           $server = $this->getClient(STEAM_SERVICE_NAME,'CommonService');
           $result = $server->storeAssistant($r);
       }catch (\Exception $e){

           return response()->json(['message'=>$e->getMessage(),'code'=> $e->getCode()]);
       }
        return response()->json(['message'=>'添加成功!','code'=>10000,$result]);
    }

    /**
     * @param Request $request
     * 讲师助教列表
     */
    public function AssistantList  ( Request $request )
    {
        try{

            $page      = isset($_GET['page'])    ?  (int)$_GET['page'] :1;
            $pageSize  = isset($_GET['pageSize']) ? (int)$_GET['pageSize'] :10;
            $user_type = isset($_GET['user_type']) ? (int)$_GET['user_type'] :1;
            $server = $this->getClient(STEAM_SERVICE_NAME,'CommonService');
            $where = ['user_type'=>$user_type,'status'=>0];
            $result = $server->getAssistantList($page,$pageSize,$where);

        }catch (\Exception $e){

            return response()->json(['message'=>$e->getMessage(),'code'=> $e->getCode()]);
        }

        return $result;

    }

    /**
     * @param Request $request
     * 编辑讲师助教
     */
    public function updateAssistant ( Request $request )
    {

        try{
            $ass_data = $request->all();
            $assistant_id = $ass_data['id'];
            $user_type = $ass_data['user_type'];
            unset($ass_data['id']);
            unset($ass_data['user_type']);
            $r = $this->__Valdate($ass_data);
            $server = $this->getClient(STEAM_SERVICE_NAME,'CommonService');
            $condition = [
                'id'=>$assistant_id,
                'user_type'=>$user_type,
                'status'=>0
            ];
            $result = $server->editAssistant($condition,$ass_data);

        }catch (\Exception $e){

            return response()->json(['message'=>$e->getMessage(),'code'=> $e->getCode()]);
         }
         return response()->json(['message'=>'修改数据成功!','code'=>10000,$result]);
    }


    /**
     * @param Request $request
     * 删除讲师助教
     */
    public function delAssistant ( Request $request )
    {
        try{

            $request_data = $request->all();
            $assistant_id  = $request_data['id'] ?? 0;
            $user_type  = $request_data['user_type'] ?? 1;
            $server = $this->getClient(STEAM_SERVICE_NAME,'CommonService');
            $where = ['id'=>$assistant_id,'user_type'=>$user_type];

            $result = $server->deleteAssistant(['status'=>1],$where);

        }catch (\Exception $e){

            return response()->json(['message'=>$e->getMessage(),'code'=> $e->getCode()]);

        }
        return response()->json(['message'=>'删除成功!','code'=>10000,$result]);

    }
    public function __Valdate( array $param )
    {
        if (empty($param['agency_id']))
        {
            throw new \Exception('机构id不能为空!', 10001 );
        }
        if (empty($param['name']))
        {
            throw new \Exception('姓名不能为空!', 10001 );
        }
        if (empty($param['url']))
        {
            throw new \Exception('图片不能为空!', 10001 );
        }
        if (empty($param['title']))
        {
            throw new \Exception('标题不能为空!', 10001 );
        }
        if (empty($param['ccontent_desc']))
        {
            throw new \Exception('描述不能为空!', 10001 );
        }
        if (empty($param['sort']))
        {
            throw new \Exception('位置不能为空!', 10001 );
        }

        return $param;
    }


}